package Sequence;

import java.util.ArrayList;

public class Test0501 {

}

class Solution0501_1 {
    ArrayList<Integer> records = new ArrayList<Integer>();
    int base;
    int count;
    int maxCount;
    public int[] findMode(TreeNode root) {
        dfs(root);

        int size = records.size();
        int[] ans = new int[size];
        for (int i = 0; i < size; i++) {
            ans[i] = records.get(i);
        }
        return ans;
    }

    public void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.left);
        update(root.val);
        dfs(root.right);
    }

    public void update(int val) {
        if (base==val){
            count++;
        }else {
            base = val;
            count = 1;
        }

        if (count == maxCount) {
            records.add(val);
        }
        if (count > maxCount) {
            maxCount = count;
            records.clear();
            records.add(val);
        }
    }
}
